package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override   //向购物车中新增
    public Cart cartAdd(Cart cart) {
        //1. 判断当前cart在数据表中是否存在
        LambdaQueryWrapper<Cart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartLambdaQueryWrapper.eq(Cart::getUserId, UserHolder.get().getId());//当前顾客的购物车
        cartLambdaQueryWrapper.eq(cart.getDishId() != null, Cart::getDishId, cart.getDishId());//菜品
        cartLambdaQueryWrapper.eq(cart.getSetmealId() != null, Cart::getSetmealId, cart.getSetmealId());//套餐
        Cart cartFromDB = cartMapper.selectOne(cartLambdaQueryWrapper);//查询

        if (cartFromDB != null) {
            //2.如果存在菜品/套餐:数量加一
            cartFromDB.setNumber(cartFromDB.getNumber() + 1);
            cartMapper.updateById(cartFromDB);
            return cartFromDB;
        } else {
            //3.如果菜品/套餐不存在
            cart.setNumber(1);//设置数量为1
            cart.setUserId(UserHolder.get().getId());//设置当前操作顾客的id
            cart.setCreateTime(new Date());
            cartMapper.insert(cart);
            return cart;
//            //要加入的菜品在购物车中不存在
//            if(cart.getDishId() == null){
//                //2.根据dish_id找到口味
//                LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, cart.getDishId());
//                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(dishFlavorLambdaQueryWrapper);
//
//                //将找到的口味信息放入cart中
//                String flavors = null;
//                if (CollectionUtil.isNotEmpty(dishFlavorList)) {
//                    for (DishFlavor dishFlavor : dishFlavorList) {
//                        flavors.contains(dishFlavor.getValue()+",");
//                    }
//                }
//                cart.setDishFlavor(flavors);
//
//                cart.setCreateTime(new Date());//创建时间
//                cartMapper.insert(cart);//插入新增加的菜品
//            }
//
//            //要加入的套餐在购物车中不存在
//            if(cart.getSetmealId() == null){
//                cart.setCreateTime(new Date());//创建时间
//                cartMapper.insert(cart);//插入新增加的套餐
//            }
        }

//        //从ThreadLocal中找到当前用户
//        Long userId = UserHolder.get().getId();
//        cart.setUserId(userId);//当前操作顾客的id
//        //当前加入购物车的是 菜品
//        if (cart.getDishId() != null) {
//            //查看购物车中是否已经有了该菜品
//            LambdaQueryWrapper<Cart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            cartLambdaQueryWrapper.eq(Cart::getUserId, userId);//顾客的购物车
//            cartLambdaQueryWrapper.eq(Cart::getDishId, cart.getDishId());
//            Integer count = cartMapper.selectCount(cartLambdaQueryWrapper);//count只会是0或者1
//            if (count > 0) {//要加入的菜品在购物车中已经存在
//                //数量增加即可
//                cart = cartMapper.selectOne(cartLambdaQueryWrapper);
//                cart.setNumber(cart.getNumber() + 1);
//            } else {//要加入的菜品在购物车中不存在
//                //1.设置数量为1
//                cart.setNumber(1);
//
//                //2.根据dish_id找到口味
//                LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, cart.getDishId());
//                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(dishFlavorLambdaQueryWrapper);
//
//                String flavors = null;
//                if (CollectionUtil.isNotEmpty(dishFlavorList)) {
//                    for (DishFlavor dishFlavor : dishFlavorList) {
//                        flavors.contains(dishFlavor.getValue()+",");
//                    }
//                }
//                cart.setDishFlavor(flavors);
//
//                //更新时间
//                cart.setCreateTime(new Date());
//                //插入新增加的菜品
//                cartMapper.insert(cart);
//            }
//        }
//
//        //当前加入购物车的是 套餐
//        if (cart.getSetmealId() != null) {
//            //查看购物车中是否已经有了该套餐
//            LambdaQueryWrapper<Cart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            cartLambdaQueryWrapper.eq(Cart::getUserId, userId);//顾客的购物车
//            cartLambdaQueryWrapper.eq(Cart::getSetmealId, cart.getSetmealId());
//            Integer count = cartMapper.selectCount(cartLambdaQueryWrapper);//count只会是0或者1
//
//            if (count > 0) {//要加入的套餐在购物车中已经存在
//                //数量增加即可
//                cart = cartMapper.selectOne(cartLambdaQueryWrapper);
//                cart.setNumber(cart.getNumber() + 1);
//            } else {//要加入的套餐在购物车中不存在
//                //设置数量为1
//                cart.setNumber(1);
//                //更新时间
//                cart.setCreateTime(new Date());
//                //插入新增加的套餐
//                cartMapper.insert(cart);
//            }
//        }
//        return cart;
    }


    @Override   //获取购物车内商品的集合
    public List<Cart> findCart() {
        LambdaQueryWrapper<Cart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartLambdaQueryWrapper.eq(Cart::getUserId, UserHolder.get().getId());//当前顾客的购物车
        List<Cart> cartList = cartMapper.selectList(cartLambdaQueryWrapper);

        return cartList;
    }

    @Override       //删除购物车中内容
    public Cart cartDelete(Cart cart) {
        //从顾客的购物车中查询到信息
        LambdaQueryWrapper<Cart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartLambdaQueryWrapper.eq(Cart::getUserId, UserHolder.get().getId());//当前顾客的购物车
        cartLambdaQueryWrapper.eq(cart.getDishId() != null, Cart::getDishId, cart.getDishId());
        cartLambdaQueryWrapper.eq(cart.getSetmealId() != null, Cart::getSetmealId, cart.getSetmealId());

        Cart cartFromDB = cartMapper.selectOne(cartLambdaQueryWrapper);

        //数量减一
        cartFromDB.setNumber(cartFromDB.getNumber() - 1);
        if(cartFromDB.getNumber() <= 0){
            //如果该购物车中该条数据为0,则应该从购物车中删除
            cartMapper.deleteById(cartFromDB);
        }else{
            cartMapper.updateById(cartFromDB);//更新数据库
        }
        return cartFromDB;
    }

    @Override    //清空购物车
    public void cartClean() {
        LambdaQueryWrapper<Cart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartLambdaQueryWrapper.eq(Cart::getUserId, UserHolder.get().getId());//当前顾客的购物车
        //删除当前顾客的购物车内容
        cartMapper.delete(cartLambdaQueryWrapper);
    }


}
